<template>
  <div>
    <!-- from表单 -->
    <el-dialog
      title="修改信息"
      :visible.sync="show"
      width="50%"
      :before-close="closeDialog"
    >
      <div class="from-infomations">
        <el-form ref="form" label-position="left" :model="userInfo" label-width="10rem">
          <el-form-item label="工号">
            {{ userInfo.userId }}
          </el-form-item>
          <el-form-item label="姓名">
            {{ userInfo.userinfo.userName }}
          </el-form-item>
          <el-form-item label="所属党小组">
            {{ userInfo.userinfo.userBranch }}
          </el-form-item>
          <el-form-item label="当前总分">
            {{ userInfo.userinfo.userScore }}
          </el-form-item>
          <el-form-item label="请选择分数类型">
            <el-select v-model="scoreRules" style="width:13rem" placeholder="请选择" :change="getScoreType()">
              <el-option
                v-for="item in scoreAllRule"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <!-- 输入分数 -->
          <el-form-item v-if="scoreRules !== ''" label="分数">
            <el-tooltip class="form-score" effect="dark" :content="scoreTips" placement="bottom-start">
              <el-input
                v-model="updateScore"
                style="width:13rem"
                placeholder="请输入分数"
                clearable
                oninput="value=value.replace(/^\.+|[^\d.-]/g,'')"
                maxlength="4"
                show-word-limit
                @input="changeSendScore"
              />
            </el-tooltip>
          </el-form-item>
          <el-form-item v-if="scoreRules === ''" label="分数">
            请选择评分机制，选择后方可输入分数
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取 消</el-button>
        <el-button type="primary" @click="sendDialog()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { updateUserInfo } from '../../api/user.js'
import { getAddSend } from '../../api/send.js'
export default {
  props: {
  },
  data() {
    return {
      // 获取的动态数据
      userInfo: {
        userId: '',
        userPwd: '',
        userType: '',
        userTypeName: '',
        userinfo: {
          userName: '',
          userBranch: '',
          userReconsider: '',
          userScore: ''
        }
      },
      // 需要更新的党小组信息
      userUpdateScore: '',
      scoreRules: '',
      scoreAllRule: [
        {
          value: '1',
          label: '加分制'
        },
        {
          value: '2',
          label: '扣分制'
        }
      ],
      scoreTips: '',
      sendScore: '',
      updateScore: '',
      // 需要更新的权限信息
      userNewType: [],

      // 页面存储数据
      show: false
    }
  },
  created() {
    // dialog信息初始化
    this.createDialogStatus()
  },
  methods: {
    // dialog信息初始化
    createDialogStatus() {
      this.show = false
    },
    // 展示dialog
    showDialog(data) {
      this.show = true
      var that = this
      //  获取用户信息
      that.userInfo = data
      that.oldUserName = data.userinfo.userName
      that.oldScore = data.userinfo.userScore
    },
    // 关闭dialog
    closeDialog() {
      var that = this
      this.$confirm('是否确定取消', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.userInfo.userinfo.userScore = that.oldScore
        this.userUpdateScore = that.oldScore
        this.show = false
        this.scoreRules = ''
        this.updateScore = ''
        // 判断是否输入了分数，删除它
        this.$message({
          type: 'success',
          message: '取消修改成功'
        })
      }).catch(() => {})
    },
    // 提交dialog
    sendDialog() {
      var that = this
      // 做拦截
      if (that.updateScore === '') {
        that.$message({
          type: 'warning',
          message: '请输入成绩后提交'
        })
      } else {
        new Promise(function(resolve, reject) {
          that.$confirm('是否确定提交', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            that.$message('正在提交中')
            that.updateUserInfo()
            resolve()
          }).catch(() => {
            resolve()
          })
        }).then(() => {

        })
      }
    },
    // 更新userInfo信息
    updateUserInfo() {
      // 获取全部数据
      var that = this
      var userInfo = that.userInfo
      var userBranch = that.userNewBranch
      // 判断分数状态
      var scoreRules = that.scoreRules
      var userScore = ''
      var userSendScore = ''
      if (scoreRules === '1') {
        userSendScore = parseFloat(that.updateScore)
        userScore = parseFloat(that.updateScore) + parseFloat(that.userInfo.userinfo.userScore)
      } else if (scoreRules === '2') {
        userSendScore = -parseFloat(that.updateScore)
        userScore = parseFloat(that.userInfo.userinfo.userScore) - parseFloat(that.updateScore)
      }

      var userUpdateInfo = {
        userId: '',
        userName: '',
        userReconsider: '',
        userScore: '',
        userBranch: ''
      }
      // 更新信息
      userUpdateInfo.userId = userInfo.userId
      userUpdateInfo.userName = userInfo.userinfo.userName
      userUpdateInfo.userReconsider = '2'
      userUpdateInfo.userScore = userScore
      userUpdateInfo.userBranch = userBranch
      var sendInfo = {
        sendUserId: '',
        sendFlistId: '',
        sendSlistId: '',
        sendTlistId: '',
        sendScore: '',
        sendGiveUserId: ''
      }
      sendInfo.sendUserId = userInfo.userId
      sendInfo.sendFlistId = '58'
      sendInfo.sendSlistId = '58'
      sendInfo.sendTlistId = '58'
      sendInfo.sendScore = userSendScore
      sendInfo.sendGiveUserId = sessionStorage.getItem('userId')
      sendInfo.sendSemseterId = sessionStorage.getItem('semId')
      new Promise(function(resolve, reject) {
        updateUserInfo(userUpdateInfo).then(res => {
          resolve(res.msg)
        })
      }).then((data) => {
        if (data === '操作成功') {
          getAddSend(sendInfo).then(res => {
            if (res.obj === null) {
              that.$message({
                message: '修改成功',
                type: 'success'
              })
              setTimeout(function() {
                that.show = false
                top.location.reload()
              }, 1000)
            } else {
              that.$message({
                message: '服务器异常，请稍后重试',
                type: 'error'
              })
            }
          })
        } else {
          that.$message({
            message: '服务器异常，请稍后重试',
            type: 'error'
          })
        }
      })
    },

    // 查看当前分数类型
    getScoreType() {
      var that = this
      var scoreRules = that.scoreRules
      if (scoreRules === '1') {
        that.scoreTips = '当前为加分制，输入1即代表加1分'
      } else if (scoreRules === '2') {
        that.scoreTips = '当前为扣分制，输入1即代表扣1分'
      }
    },
    // 获取当前分数
    changeSendScore(e) {
      var that = this
      that.updateScore = e
    }

  }
}
</script>
<style lang="scss">

</style>
